package org.example.invest.utils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import org.fisco.bcos.sdk.transaction.model.dto.CallResponse;

/**
 * 处理CallResponse返回值的工具类
 */
public class CallResponseUtils {

    /**
     * 从CallResponse中安全获取Boolean类型的返回值
     *
     * @param response CallResponse
     * @param defaultValue 默认值
     * @return Boolean值
     */
    public static Boolean getBooleanValue(CallResponse response, Boolean defaultValue) {
        try {
            if (response != null && response.getValues() != null) {
                Object obj = response.getValues();
                if (obj instanceof Boolean) {
                    return (Boolean) obj;
                } else if (obj instanceof List) {
                    List<?> list = (List<?>) obj;
                    if (!list.isEmpty() && list.get(0) instanceof Boolean) {
                        return (Boolean) list.get(0);
                    }
                }
            }
        } catch (Exception e) {
            // 忽略异常，返回默认值
        }
        return defaultValue;
    }

    /**
     * 从CallResponse中安全获取BigInteger类型的返回值
     *
     * @param response CallResponse
     * @param defaultValue 默认值
     * @return BigInteger值
     */
    public static BigInteger getBigIntegerValue(CallResponse response, BigInteger defaultValue) {
        try {
            if (response != null && response.getValues() != null) {
                Object obj = response.getValues();
                if (obj instanceof BigInteger) {
                    return (BigInteger) obj;
                } else if (obj instanceof List) {
                    List<?> list = (List<?>) obj;
                    if (!list.isEmpty() && list.get(0) instanceof BigInteger) {
                        return (BigInteger) list.get(0);
                    }
                }
            }
        } catch (Exception e) {
            // 忽略异常，返回默认值
        }
        return defaultValue;
    }

    /**
     * 从CallResponse中安全获取List类型的返回值
     *
     * @param response CallResponse
     * @return List值，如果获取失败则返回空列表
     */
    public static List<Object> getListValues(CallResponse response) {
        try {
            if (response != null && response.getValues() != null) {
                Object obj = response.getValues();
                if (obj instanceof List) {
                    return (List<Object>) obj;
                } else {
                    List<Object> result = new ArrayList<>();
                    result.add(obj);
                    return result;
                }
            }
        } catch (Exception e) {
            // 忽略异常，返回空列表
        }
        return new ArrayList<>();
    }
    
    /**
     * 工具方法：将16进制字符串转换为标准的bytes32字节数组
     * 这个方法确保所有地方使用一致的字节数组格式
     *
     * @param hexString 16进制字符串，可以带有0x前缀
     * @return 标准化的32字节数组
     */
    public static byte[] standardizeBytes32(String hexString) {
        // 去除0x前缀
        if (hexString.startsWith("0x")) {
            hexString = hexString.substring(2);
        }
        
        // 确保长度是偶数
        if (hexString.length() % 2 != 0) {
            hexString = "0" + hexString;
        }
        
        // 标准化长度为64个字符（32字节的16进制表示）
        if (hexString.length() > 64) {
            // 如果过长，截取前64个字符
            hexString = hexString.substring(0, 64);
        } else if (hexString.length() < 64) {
            // 如果过短，左填充0
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < 64 - hexString.length(); i++) {
                sb.append('0');
            }
            sb.append(hexString);
            hexString = sb.toString();
        }
        
        // 转换为字节数组
        byte[] result = new byte[32]; // bytes32固定为32字节
        for (int i = 0; i < 64; i += 2) {
            int high = Character.digit(hexString.charAt(i), 16);
            int low = Character.digit(hexString.charAt(i+1), 16);
            
            if (high == -1 || low == -1) {
                throw new IllegalArgumentException("无效的16进制字符串: " + hexString);
            }
            
            result[i/2] = (byte) ((high << 4) + low);
        }
        
        return result;
    }
} 